#ifndef FNCTNSMTH
#define FNCTNSMTH

#define P1M1

//#define IsEqual(a,b) ((a) < (b) ? ((b) - (a)) < 1e-5 : ((a) - (b)) < 1e-5)
//#define IS_NOT_EQUAL(a,b) ((a) < (b) ? ((b) - (a)) > 1e-5 : ((a) - (b)) > 1e-5)

#ifndef  __MATH_H
#include <math.h>
#endif

extern double TendsVersPlusInfini;
extern double TendsVersMoinsInfini;
extern double TendsVersZero;
inline bool IsEqual (double a, double b) {return (( a > b ? a - b : b - a) < 1.0e-9 ? true : false);}
inline bool IS_NOT_EQUAL (double a, double b) {return (( a > b ? a - b : b - a) < 1.0e-9 ? false : true);}
inline double Fabs (double v) {return v < 0.0 ? -v : v;}
double max (double a, double b);

double GetProbNormCumFor (double moyenne, double ecartType, double valeur);

double GetEvalNormalisee (double objMin, double objMax, double v);
double GetEvalM2P2 (double v);
void InitTEvalNormalisee (void);

double GetDeriveeEvalNormalisee (double objMin, double objMax, double v);
double GetDeriveeEvalM2P2 (double v);
void InitTDerivEvalNormalisee (void);

double GetEvalNormalisee01 (double objMin, double objMax, double v);
double GetEval01M2P2 (double v);
double GetEval01M1P1 (double v);
double GetProbabiliteNormale (double moyenne, double ecartType, double valeur);
double GetValeurProbable (double min, double max, double precision, double va, double ecartType, int nbTranchesMax = 100, bool withExtremes = false);
double GetValeurClasseFloue (double moyenne, double ecartType, double valeur);
double GetValeurCroisementProbable (double min, double max, double precision, double va, double vb, double ecartType, int nbTranchesMax = 100, bool withExtremes = false);
double GetValeurCroisementProbablePond (double min, double max, double precision, double va, double poidsA, double etA, double vb, double poidsB, double etB, int nbTranchesMax = 100, bool withExtremes = false);
double CalcLimOpposee (double limite, double centre, double ecart);
void LoadCapteurFlou (double * tVal, bool * tFlag, int nbV, double vIn, double min, double max, double vCenter);
void LoadCapteurFlou (double * tVal, int nbV, double vIn, double min, double max, double vCenter, double etClasses = 5.0);
void LoadCapteurFlouCos (double * tVal, int nbV, double vIn, double min, double max, double vCenter);
void LoadCapteurFlouCosCont (double * tVal, int nbV, double vInS, double vInE, double min, double max, double vCenter);
void MultLoadCapteurFlouCos (double * tVal, int nbV, double vIn, double min, double max, double vCenter);
double GetDistFromTo (double xa, double ya, double za, double xb, double yb, double zb);
double GetDist2FromTo (double xa, double ya, double za, double xb, double yb, double zb);
double EquilibreDistance (double &xa, double &ya, double &za, double &xb, double &yb, double &zb, double poids, double distCible);
void EloignerP3D (double &xa, double &ya, double &za, double &xb, double &yb, double &zb, double puiss, double distMin);
void RaprocherP3D (double &xa, double &ya, double &za, double &xb, double &yb, double &zb, double puiss, double distMin);
void SetDistTo (double &xa, double &ya, double &za, double dist);
void SetMaxDistTo (double &xa, double &ya, double &za, double dist);
double TabSin (double degres);
double TabCos (double degres);
void Polaire2Cartesien (double longitude, double latitude, double & x, double & y, double & z);
double myExp (double x);
double Objectif2Prediction (double objectif, double valeur, double coef);
double Prediction2Objectif (double prediction, double valeur, double coef);
double PoidsPrediction2Objectif (double objectif, double valeur, double coef);
void AppliqueForceRessort (double &xa, double &ya, double &za, double &xb, double &yb, double &zb, double raideur, double distNormale);
void Attirer (double &xa, double &ya, double &za, double &xb, double &yb, double &zb, double raideur);
double PctProgressionCroissant (double a, double b);

class MemoireTraceur {
public :
	MemoireTraceur (int duree);
  ~MemoireTraceur (void);

  void Reset (void);
	void SetDuree (int duree);
  void AddValue (double v);
  double GetRelValue (int indice, bool enPourcentage = true);
  double GetAbsValue (int indice, bool enPourcentage = true);
  inline int GetNbElems (void) {return NbElems;}
  inline double GetMin (void) {return Min;}
  inline double GetMax (void) {return Max;}
  inline int GetLastIndice (void) {return LastIndice;}
private :
	int Duree;
  double * Valeurs;
  int LastIndice;
  int NbElems;
	double Min;
  double Max;
};

class Stats {
public :
	Stats (void);
	void Reset (void);

	void AddValue (double v);
	void AddValuePond (double v, double poids);
  inline double GetDN (void) {return DN;}
  inline double GetSomme (void) {return Somme;}
  inline double GetMin (void) {return Min;}
  inline double GetMax (void) {return Max;}
  double GetMoyenne (void);
  double GetVariance (void);
  double GetEcartType (void);
  double GetLimSupET (double coefET);
  inline double GetDernier (void) {return Dernier;}
  inline double GetDerivee (void) {return Derivee;}
  inline double GetAcceleration (void) {return Acceleration;}
  void TraceDonnees (void);

private :
	bool CalcOK;
	double DN;
  double Dernier;
  double Derivee;
  double Acceleration;
  double Somme;
  double SommeCarres;
  double Min;
  double Max;
  double Variance;
  double EcartType;
  double Moyenne;

	void CalcStats (void);

};
#endif
